package cn.dnui_yxk801.dnuifood_16110100801.controller;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.List;

import cn.dnui_yxk801.dnuifood_16110100801.R;
import cn.dnui_yxk801.dnuifood_16110100801.listener.OnRetrofitListener;
import cn.dnui_yxk801.dnuifood_16110100801.model.ShopModel;


/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment3 extends Fragment implements OnRetrofitListener{
    private EditText et_search;
    private Button btn_search;
    private Button btn_dl_search;
    private String search;
    private ListView lv_search;
    private View view;
    private RecordSQLiteOpenHelper recordSQLiteOpenHelper;
    private RecordDBOperation recordDBOperation;
    private String shopname;


    public Fragment3() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment3, container, false);
        initViews();
        initEvents();
        return view;
    }


    private void initViews(){
        et_search=view.findViewById(R.id.et_search);
        btn_search=view.findViewById(R.id.btn_search);
        btn_dl_search=view.findViewById(R.id.btn_dl_search);
        lv_search=view.findViewById(R.id.lv_search);
        recordSQLiteOpenHelper=new RecordSQLiteOpenHelper(getActivity());
        recordDBOperation=new RecordDBOperation(recordSQLiteOpenHelper);
    }
    private void initEvents(){
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recordDBOperation.insertData(et_search.getText().toString().trim());
                Intent intent=new Intent(getActivity(),SearchResultActivity.class);
                search=et_search.getText().toString();
                intent.putExtra("search",search);
                startActivity(intent);
            }
        });
        btn_dl_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recordDBOperation.deleteData();
                showData("");
            }
        });
        et_search.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode==KeyEvent.KEYCODE_ENTER&&event.getAction()==KeyEvent.ACTION_DOWN){
                    ((InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
                    boolean hasData=recordDBOperation.hasData(et_search.getText().toString().trim());
                    if (!hasData){
                        recordDBOperation.insertData(et_search.getText().toString().trim());
                        showData("");
                    }
                    Intent intent=new Intent(getActivity(),SearchResultActivity.class);
                    search=et_search.getText().toString();
                    intent.putExtra("search",search);
                    startActivity(intent);
                }
                return false;
            }
        });
        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable s) {
            btn_search.setVisibility(View.VISIBLE);
            String tempName=et_search.getText().toString();
            showData(tempName);
            }
        });
        lv_search.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView textView=(TextView) view.findViewById(android.R.id.text1);
                String name=textView.getText().toString();
                et_search.setText(name);
            }
        });
    }

    void showData(String tempName){
        List list=recordDBOperation.queryData(tempName);

        SimpleAdapter adapter=new SimpleAdapter(getActivity(),list,android.R.layout.simple_list_item_1,new String[]{"name"},new int[]{android.R.id.text1});
        lv_search.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onSuccess(Object object, int flag) {

    }

    @Override
    public void onFalure(String msg) {

    }
}
